package com.bjpowernode.crm.settings.web.controller;

import com.bjpowernode.crm.commons.constants.Constant;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class userController {

    @Autowired
    private UserService userService;

    //跳转到登录页面的controller
    @RequestMapping("/settings/qx/user/toLogin.do")
    public String toLogin(){
        return "settings/qx/user/login";
    }

    @RequestMapping("/settings/qx/user/login.do")
    @ResponseBody
    public Object login(String loginAct, String loginPwd, String isRemPwd, HttpServletRequest request, HttpServletResponse response, HttpSession session){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("loginAct",loginAct);
        map.put("loginPwd",loginPwd);
        User user = userService.queryUserByLoginAndPwd(map);

        ReturnObject returnObject = new ReturnObject();

        if(user == null){
            //登陆失败，账号或者密码错误
            returnObject.setCode(Constant.RETURN_CODE_FAIL);
            returnObject.setMessage("用户名或者密码错误");
        }else{
            if(DateUtils.formateDataTime(new Date()).compareTo(user.getExpireTime())>0){
                //登陆失败，时间过期
                returnObject.setCode(Constant.RETURN_CODE_FAIL);
                returnObject.setMessage("账号已过期！");
            }else if("0".equals(user.getLockState())){
                //登陆失败，用户被锁
                returnObject.setCode(Constant.RETURN_CODE_FAIL);
                returnObject.setMessage("用户状态为被锁状态！");
            }else if(!user.getAllowIps().contains(request.getRemoteAddr())){
                //登陆失败，用户使用的ip不再常用ip中
                returnObject.setCode(Constant.RETURN_CODE_FAIL);
                returnObject.setMessage("用户不在常用的ip下登录！");
            }else {
                returnObject.setCode(Constant.RETURN_CODE_SUCCESS);
                session.setAttribute(Constant.SESSION_USER,user);

                //判断是否记住账号密码
                if("true".equals(isRemPwd)){
                    //记住了，向外发送包含账号密码的cookie
                    Cookie c1 = new Cookie("loginAct", user.getLoginAct());
                    //设置生命周期为10天
                    c1.setMaxAge(10*24*60*60);
                    //发送cookie(通过response)
                    response.addCookie(c1);
                    Cookie c2 = new Cookie("loginPwd", user.getLoginPwd());
                    c2.setMaxAge(10*24*60*60);
                    response.addCookie(c2);
                }else{
                    //没记住，就要删除cookie，将cookie生命周期改为0
                    Cookie c1 = new Cookie("loginAct", "1");
                    c1.setMaxAge(0);
                    response.addCookie(c1);
                    Cookie c2 = new Cookie("loginPwd", "1");
                    c2.setMaxAge(0);
                    response.addCookie(c2);
                }
            }
        }
        return returnObject;
    }


    //安全退出
    @RequestMapping("/settings/qx/user/logout.do")
    public String logout(HttpServletResponse response,HttpSession session){
        //删除cookie
        Cookie c1 = new Cookie("loginAct", "1");
        c1.setMaxAge(0);
        response.addCookie(c1);
        Cookie c2 = new Cookie("loginPwd", "1");
        c2.setMaxAge(0);
        response.addCookie(c2);

        //删除session中的User
        session.invalidate();

        //3.重定向到登录界面
        return "redirect:/";
    }
}
